package cz.muni.fi.pa165.bean;

import cz.muni.fi.pa165.dto.CustomerDTO;
import cz.muni.fi.pa165.service.local.impl.CustomerService;
import cz.muni.fi.pa165.shiro.ShiroSecured;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author shifty
 */
@RequestScoped
@Named
public class CustomerManager implements Serializable{
    
    final static Logger log = LoggerFactory.getLogger(CustomerManager.class);
    
    @Inject
    CustomerService customerService;
    
    private String searchField;

    private Long id;
    private String name;
    private String address;
    private String zip;
    private String city;
    private String country;
    private String accountNumber;
    
    private boolean updateSuccess;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }
    
    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }
    public boolean isUpdateSuccess() {
        return updateSuccess;
    }
    
    public void createCustomer() {
        CustomerDTO customer = new CustomerDTO();
        customer.setName(name);
        customer.setAddress(address);
        customer.setZip(zip);
        customer.setCity(city);
        customer.setCountry(country);
        customer.setAccountNumber(accountNumber);
        customerService.createCustomer(customer);
        clearForm();
    }
    
    public void updateCustomer() {
        CustomerDTO customer = new CustomerDTO();
        customer.setId(id);
        customer.setName(name);
        customer.setAddress(address);
        customer.setZip(zip);
        customer.setCity(city);
        customer.setCountry(country);
        customer.setAccountNumber(accountNumber);
        customerService.updateCustomer(customer);
        clearForm();
        updateSuccess=true;
    }
    
    private void clearForm() {
        name=null;
        address=null;
        zip=null;
        city=null;
        country=null;
        accountNumber=null;
    }
   
    public void loadCustomer(Long id) {
        this.id=id;
        CustomerDTO customer = customerService.getCustomerById(id);
        if(customer != null) {
            name=customer.getName();
            address=customer.getAddress();
            zip=customer.getZip();
            city=customer.getCity();
            country=customer.getCountry();
            accountNumber=customer.getAccountNumber();
        }     
    }
}
